﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZSZ.DTO;
using ZSZ.Iservice;
using ZSZ.Service.Entity;

namespace ZSZ.Service
{
    public class PermissionService : IPermissionService
    {
        public void AddPermIds(long roleId, long[] permIds)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<RoleEntity> cs = new CommonService<RoleEntity>(ct);
                var role = cs.GetById(roleId);
                if (role == null)
                {
                    throw new ArgumentException("没有这个role");
                }
                var pers = ct.Permissions.Where(p => p.IsDeleted == false && permIds.Contains(p.Id)).ToList();
                foreach (var item in pers)
                {
                    role.Permissions.Add(item);
                }
                ct.SaveChanges();
            }
        }
        private PermissionDTO ToDTO(PermissionEntity e)
        {
            PermissionDTO dto = new PermissionDTO();
            dto.CreateTime = e.CreateDatatime;
            dto.Description = e.Description;
            dto.Id = e.Id;
            dto.Name = e.Name;
            return dto;
        }
        public PermissionDTO[] GetAll()
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<PermissionEntity> cs = new CommonService<PermissionEntity>(ct);
                return cs.GetAll().ToList().Select(p => ToDTO(p)).ToArray();
            }
        }

        public PermissionDTO GetById(long id)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<PermissionEntity> cs = new CommonService<PermissionEntity>(ct);
                PermissionEntity e = cs.GetById(id);
                if (e == null)
                {
                    return null;
                }
                return ToDTO(e);
            }
        }

        public PermissionDTO GetByName(string name)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<PermissionEntity> cs = new CommonService<PermissionEntity>(ct);
                PermissionEntity e = cs.GetAll().SingleOrDefault(p => p.Name == name);
                if (e == null)
                {
                    return null;
                }
                return ToDTO(e);
            }
        }

        public PermissionDTO[] GetByRoleId(long roleId)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<RoleEntity> cs = new CommonService<RoleEntity>(ct);
                RoleEntity role = cs.GetById(roleId);
                return role.Permissions.Select(p => ToDTO(p)).ToArray();
            }
        }
        public void UpdatePermIds(long roleId, long[] permIds)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<RoleEntity> cs = new CommonService<RoleEntity>(ct);
                var role = cs.GetById(roleId);
                role.Permissions.Clear();
                var ps = ct.Permissions.Where(p => p.IsDeleted == false && permIds.Contains(p.Id));
                foreach (var item in ps)
                {
                    role.Permissions.Add(item);
                }
                ct.SaveChanges();
            }
        }

        public long AddNew(string name, string miaoshu)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<PermissionEntity> cs = new CommonService<PermissionEntity>(ct);
                bool exits = cs.GetAll().Any(p => p.Name == name);
                if (exits)
                {
                    throw new ArgumentException("已经存在了这个权限项");
                }
                PermissionEntity en = new PermissionEntity();
                en.Name = name;
                en.Description = miaoshu;
                ct.Permissions.Add(en);
                ct.SaveChanges();
                return en.Id;
            }
        }

        public void MarkDeleted(long id)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<PermissionEntity> cs = new CommonService<PermissionEntity>(ct);
                cs.MarkDeleted(id);
            }
        }

        public void Update(long id, string name, string miaoshu)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<PermissionEntity> cs = new CommonService<PermissionEntity>(ct);
                if (!cs.GetAll().Any(s=>s.Id==id))
                {
                    throw new ArgumentException("没有这个权限项");
                }
                if (cs.GetAll().Any(s=>s.Name==name))
                {
                    throw new ArgumentException("这个名字已经被占用了");
                }
                var sss = cs.GetById(id);
                sss.Name = name;
                sss.Description = miaoshu;
                ct.SaveChanges();
            }
        }
    }
}
